<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

/**
 * 直通车报表数据API
 */

class RptAdgroupPlaceModel extends RptBaseModel {

	protected $tableName = 'rpt_adgroup_place';
	/** @var object */
	private $me = null;
	/** @var object */
	private $subwayToken = null;

	/**
	 * 初始化。主要检查session['me']。
	 *
	 */
	public function _initialize() {
		parent :: _initialize();
		$this->me = session('me');
	}
	
	/**
	 * get the summary statistics information of a adgroupplace for current user
	 * 
	 * @param int $id, 0 means total  
	 * @param $adgroup the id is an adgroup_id or a campain_id
	 * @param string|array $indicators the indicators needed: 
	 * 						  impressions 展现量,roi,投入产出比 = sales / cost , cost 花费 ,
	 * 						  direct_pay , 直接成交 , indirect_pay 间接成交
	 * 						  direct_pay_count , 直接成交数, indirect_pay_count 间接成交数
	 * 						  roc 转化率 =产生购买行为的客户人数 / 所有到达店铺的访客人数
	 * 						  sales 销售额=direct_pay + indirect_pay, click  点击量 , ctr 点击率 , cpm 千次展现费 ;
	 * 						  avgpos 平均展现排名
	 * @param string $from  date string of the start date , format Y-m-d H:i:s , 
	 * @param string $to date strign of the end date , format  Y-m-d H:i:s
	 * @param string $orderby the order by indicator
	 * @param string $desc order by desc or asc,  
	 *		
	 * 
	 * @return mixed of array, 
	 * 					example:
	 *  
	 * 
	 */
	 public function getPlaceStat($user_id, $adgroup_id, $indicators= 'all',
	 		$from = null, $to = null, $orderby='impressions', $desc = true, $pageNo=1, $pageSize=100) {
	 			
 		$stats = $this->getSummaryStats($user_id, 'adgroup_id', $adgroup_id, $indicators, $from, $to); 
		$return = array (
 				'user_id' => $user_id,
 				'from' => strtotime($from),
 				'to' => strtotime($to),
 				'pageNo'=>$pageNo,
 				'pageSize'=>$pageSize,
 				'totalNumber'=>count($stats),
 				'data'=>$stats,
 		);
 			
 		return $return;
    }

    /**
     * 同步推广组定向投放报表. 每次更新一个campaign的所有adgroups.
     * TODO campaign_id和adgroup_id本应该都是可选的, 但是目前只支持到campaign这个粒度.
     * 
     * @param int $user_id
     * @param int $campaign_id 
     * @return bool , true when 成功  ,false时没有任何更新
     */
    public function syncAdgroupPlaceReport($user_id , $campaign_id){ //  , $adgroup_id = 0
    	if(empty($user_id) || $campaign_id <= 0)
    		throw_exception('参数错误: 必须全部给定且大于0.');
    	
		//$entity = 'adgroup_place_' . $adgroup_id;
    	$entity = 'rpt-campaign-place:' . $campaign_id;
    	$adgroup_id = 0;
		
        $time_slot = SyncModel::getSyncTimeSlot($user_id, $entity);
		if($time_slot){
			$rptMdl = new RptModel();
			$rptMdl->syncSummaryData($user_id, 'rpt_adgroup_place', $campaign_id , $adgroup_id, $time_slot['start'] , $time_slot['end']);
		}else{
			return false;
		}
		
		$condition = array(
				'user_id' => $user_id, 
				'camapaign_id' => $campaign_id,
		); 
		
		//查找最后插入成功的时间
		$updated = $this->where($condition)->order('date DESC')->getField('date');
		if($updated) {
			SyncModel::setLatestUpdated( $user_id , $entity , $updated);
		}
		return true;
    }
	
}